package school.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import school.entity.Grade;
import school.util.DBUtil;

public class GradeDao {
	public static final GradeDao DAO = new GradeDao();

	// public List<Grade> findALL() {
	// return DBUtil.query("select * from grade order by id", Grade.class);
	// }
	public List<Grade> findALL() {
		String sql = "select * from `grade` order by id asc";

		List<Grade> datas = new ArrayList<Grade>();
		// 如果表为空，返回空集合，不是null
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				// 读取一行，封装成Student对象，放入集合
				Grade st = new Grade();
				st.setId(rs.getLong("id"));
				st.setGname(rs.getString("gname"));
				// ..........
				// !!!!!!!!!!!
				datas.add(st);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, con);
		}
		return datas;

	}
}
